<?php
require_once ("BaseDAO.php");

// Contact Value Object
class Contact extends BaseVO {
	var $id;
	var $firstname;
	var $lastname;
	var $work_phone;
	var $fax;
	var $home_phone;
	var $cell_phone;
	var $email;
	var $address_id;

	// create a new VO
	function Contact(
		$id= 0,
		$firstname= "",
		$lastname= "",
		$work_phone= "",
		$fax= "",
		$home_phone= "",
		$cell_phone= "",
		$email= "",
		$address_id= 0
	) {
		$this->id = $id;
		$this->firstname = $firstname;
		$this->lastname = $lastname;
		$this->work_phone = $work_phone;
		$this->fax = $fax;
		$this->home_phone = $home_phone;
		$this->cell_phone = $cell_phone;
		$this->email = $email;
		$this->address_id = $address_id;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->firstname == $vo->firstname &&
			$this->lastname == $vo->lastname &&
			$this->work_phone == $vo->work_phone &&
			$this->fax == $vo->fax &&
			$this->home_phone == $vo->home_phone &&
			$this->cell_phone == $vo->cell_phone &&
			$this->email == $vo->email &&
			$this->address_id == $vo->address_id;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->firstname = $vo->firstname;
		$this->lastname = $vo->lastname;
		$this->work_phone = $vo->work_phone;
		$this->fax = $vo->fax;
		$this->home_phone = $vo->home_phone;
		$this->cell_phone = $vo->cell_phone;
		$this->email = $vo->email;
		$this->address_id = $vo->address_id;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->firstname . "," . $this->lastname . "," . $this->work_phone . "," . $this->fax . "," . $this->home_phone . "," . $this->cell_phone . "," . $this->email . "," . $this->address_id;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<firstname>$this->firstname</firstname>\n" .
		"<lastname>$this->lastname</lastname>\n" .
		"<work_phone>$this->work_phone</work_phone>\n" .
		"<fax>$this->fax</fax>\n" .
		"<home_phone>$this->home_phone</home_phone>\n" .
		"<cell_phone>$this->cell_phone</cell_phone>\n" .
		"<email>$this->email</email>\n" .
		"<address_id>$this->address_id</address_id>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->firstname = $this->formHelper($prefix . "firstname", "");
		$this->lastname = $this->formHelper($prefix . "lastname", "");
		$this->work_phone = $this->formHelper($prefix . "work_phone", "");
		$this->fax = $this->formHelper($prefix . "fax", "");
		$this->home_phone = $this->formHelper($prefix . "home_phone", "");
		$this->cell_phone = $this->formHelper($prefix . "cell_phone", "");
		$this->email = $this->formHelper($prefix . "email", "");
		$this->address_id = $this->formHelper($prefix . "address_id", 'null');
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->firstname = $this->queryHelper($prefix . "firstname", "");
		$this->lastname = $this->queryHelper($prefix . "lastname", "");
		$this->work_phone = $this->queryHelper($prefix . "work_phone", "");
		$this->fax = $this->queryHelper($prefix . "fax", "");
		$this->home_phone = $this->queryHelper($prefix . "home_phone", "");
		$this->cell_phone = $this->queryHelper($prefix . "cell_phone", "");
		$this->email = $this->queryHelper($prefix . "email", "");
		$this->address_id = $this->queryHelper($prefix . "address_id", 'null');
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// Contact Data Access Object
class ContactDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `contact` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `contact` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `contact` (firstname,lastname,work_phone,fax,home_phone,cell_phone,email,address_id) VALUES ('%A','%B','%C','%D','%E','%F','%G',%H)";
	var $SQL_UPDATE = "UPDATE `contact` SET ";
	var $SQL_DELETE = "DELETE FROM `contact` WHERE id=%A";

	// default constructor
	function ContactDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single Contact obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new Contact(
				$row->id, 
				$row->firstname, 
				$row->lastname, 
				$row->work_phone, 
				$row->fax, 
				$row->home_phone, 
				$row->cell_phone, 
				$row->email, 
				$row->address_id
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of Contact objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Contact(
				$row->id, 
				$row->firstname, 
				$row->lastname, 
				$row->work_phone, 
				$row->fax, 
				$row->home_phone, 
				$row->cell_phone, 
				$row->email, 
				$row->address_id
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Contact objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Contact(
				$row->id, 
				$row->firstname, 
				$row->lastname, 
				$row->work_phone, 
				$row->fax, 
				$row->home_phone, 
				$row->cell_phone, 
				$row->email, 
				$row->address_id
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Contact objects based on the example
	function findExample($example, $orderby="") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if(strlen($example->firstname) > 0) $tmp .= "firstname like '%" . $this->safeEscapeStr($example->firstname) . "%' and ";
		if(strlen($example->lastname) > 0) $tmp .= "lastname like '%" . $this->safeEscapeStr($example->lastname) . "%' and ";
		if(strlen($example->work_phone) > 0) $tmp .= "work_phone like '%" . $this->safeEscapeStr($example->work_phone) . "%' and ";
		if(strlen($example->fax) > 0) $tmp .= "fax like '%" . $this->safeEscapeStr($example->fax) . "%' and ";
		if(strlen($example->home_phone) > 0) $tmp .= "home_phone like '%" . $this->safeEscapeStr($example->home_phone) . "%' and ";
		if(strlen($example->cell_phone) > 0) $tmp .= "cell_phone like '%" . $this->safeEscapeStr($example->cell_phone) . "%' and ";
		if(strlen($example->email) > 0) $tmp .= "email like '%" . $this->safeEscapeStr($example->email) . "%' and ";
		if($example->address_id != 0) $tmp .= "address_id = " . $this->safeEscapeStr($example->address_id) . " and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Contact(
				$row->id, 
				$row->firstname, 
				$row->lastname, 
				$row->work_phone, 
				$row->fax, 
				$row->home_phone, 
				$row->cell_phone, 
				$row->email, 
				$row->address_id
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->firstname), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->lastname), $this->sql);
		$this->sql = str_replace("%C", $this->safeEscapeStr($vo->work_phone), $this->sql);
		$this->sql = str_replace("%D", $this->safeEscapeStr($vo->fax), $this->sql);
		$this->sql = str_replace("%E", $this->safeEscapeStr($vo->home_phone), $this->sql);
		$this->sql = str_replace("%F", $this->safeEscapeStr($vo->cell_phone), $this->sql);
		$this->sql = str_replace("%G", $this->safeEscapeStr($vo->email), $this->sql);
		$this->sql = str_replace("%H", $this->safeEscapeStr($vo->address_id), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "firstname = '" . $this->safeEscapeStr($vo->firstname) . "', ";
		$this->sql .= "lastname = '" . $this->safeEscapeStr($vo->lastname) . "', ";
		$this->sql .= "work_phone = '" . $this->safeEscapeStr($vo->work_phone) . "', ";
		$this->sql .= "fax = '" . $this->safeEscapeStr($vo->fax) . "', ";
		$this->sql .= "home_phone = '" . $this->safeEscapeStr($vo->home_phone) . "', ";
		$this->sql .= "cell_phone = '" . $this->safeEscapeStr($vo->cell_phone) . "', ";
		$this->sql .= "email = '" . $this->safeEscapeStr($vo->email) . "', ";
		$this->sql .= "address_id = " . $this->safeEscapeStr($vo->address_id) . " ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

